home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++,
- Subject: Re: Tough FACTORIAL math problem...
- Date: 15 Feb 1996 22:55:38 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4g0dla$hgg@newsroom.hitc.com>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fvp9v$sso@newsroom.hitc.com>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <4fvp9v$sso@newsroom.hitc.com>, psand@eos.hitc.com says...
- >
- >In article <31224679.6193@born.com>, clelaj@born.com says...
- >>
- >>The Crow wrote:
- >>>
- >>> Here is what I am trying to do, I want to find the last NON-ZERO digi
- >t
- >> of a
- >>> given factorial. For instance,
- >>>
- >>> 5! = 120
- >>>
- >>> so the last non-zero digit is 2. I want to be able to do this up to
- >1
- >>000.
- >[SNIP!!!]
- >
- [SNIP!!!]
- >
- >If you want to minimize the testing for zero-digits, build yourself a 9x
- >9
- >matrix which holds the least-significant digit of the product of
- >(i+1)*(j+1)
- >[i,j are C/C++ indexes ranging from 0-8]. You then can use three nested
- >
- >for(...) loops and just select the row (new number lsd) and column (lsd
- >of previous factorial) and look it up...
- >
-
- OKAY, I GOOFED...(Not enough caffeine ;/)) Instead of doing the least
- significant digit, take the least significant nonzero digit...
-
- so matrix(4,5) = 2 (20 is 4*5, etc...)
-
- oooh...I'm feeling a little less brain dead, so here we go:
-
- 1 2 3 4 5 6 7 8 9
- - - - - - - - - -
- 1 | 1 2 3 4 5 6 7 8 9
- 2 | 2 4 8 6 1* 2 4 6 8
- 3 | 3 6 9 2 5 8 1 4 7
- 4 | 4 8 2 6 2* 4 8 2 6
- 5 | 5 1* 5 2* 5 3* 5 4* 5
- 6 | 6 2 8 4 3* 6 2 8 4
- 7 | 7 4 1 8 5 2 9 6 3
- 8 | 8 6 4 2 4* 8 6 4 2
- 9 | 9 8 7 6 5 4 3 2 1
-
- (the * after the number indicates I had to drop a trailing 0)
-
- Now all we got to do is take the smallest nonzero digit of the loop
- number,
- keep track of the last value found, and look it up (the table is
- symmetric-
- multiplicitive commutative stuff)...
-
- You know, this could be generalized (using more storage) to do least
- significant nonzero N digits...Of course, once N gets over about 3 you'd
- probably just forget the table...
-
- None of the values in the matrix should be zero, so if you use the least
- significant nonzero digit of the loop iterators, you should just have to
- look up a single digit number...
-
- By the way, in the generalized case (MOD J), anybody want to tackle
- extending ios to print the values (no fair going hex, decimal, or octal;
- if
- you want to do base 2, get a life...)???
-
- Hopefully, my braindeath is over...Cute problem...
-
- Oh, by the way, the least significant digit for N! base 2 is ONE, when N
- is
- 1 or larger; the rest is left as an amusing exercise for the reader...
-
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-